from rest_framework import serializers
from rest_framework.exceptions import ValidationError
from rest_framework.serializers import ModelSerializer

from book.models import BookInfo


class BookSerializer(ModelSerializer):
    id = serializers.IntegerField(help_text='ID', read_only=True)
    btitle = serializers.CharField(help_text='书名')
    bpub_date = serializers.DateField(help_text='发布日期')
    bread = serializers.IntegerField(help_text='阅读量')
    bcomment = serializers.IntegerField(help_text='评论量')

    class Meta:
        model = BookInfo
        # fields = '__all__'
        exclude = ['is_delete']

    def validate_btitle(self, value):
        if 'django' not in value.lower():
            raise ValidationError("图书不是关于Django的")
        return value


class BookSerializerForRead(ModelSerializer):
    class Meta:
        model = BookInfo
        exclude = ['id','bread','bcomment','is_delete']
